package biz.datalk.industrialland.common.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2022-12-12
 */
public class ModelAndViewUtil {

    public static ModelAndView redirectView(String url) {
        RedirectView redirectView = new RedirectView(url);
        redirectView.setStatusCode(HttpStatus.FOUND);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.FOUND);
        modelAndView.setView(redirectView);
        return modelAndView;
    }

    public static ModelAndView skipView(String url, int httpStatusCode, String errMsg) {
        if (StringUtils.isNotBlank(url)) {
            return redirectView(url);
        }

        String myErrMsg = StringUtils.isBlank(errMsg) ? "请求的资源不存在" : errMsg;
        String body = "";
        if (httpStatusCode == 403) {
            body = "<span>4</span><span>0</span><span>3</span>";
        } else if (httpStatusCode == 500) {
            body = "<span>5</span><span>0</span><span>0</span>";
        } else if (httpStatusCode == 400) {
            body = "<span>4</span><span>0</span><span>0</span>";
        } else {
            httpStatusCode = 404;
            body = "<span>4</span><span>0</span><span>4</span>";
        }


        StringBuffer sb = new StringBuffer();
        sb.append("<!DOCTYPE html>");
        sb.append("<html lang=\"zh\">");
        sb.append("<head>");
        sb.append("<meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\"content=\"IE=edge\"><meta name=\"viewport\"content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"><meta name=\"referrer\"content=\"never\">");
        sb.append("<title>").append(httpStatusCode).append("</title>");
        sb.append("<link rel=\"shortcut icon\"href=\"/favicon.ico\"><style>html,body{height:100%;margin:0;padding:0;background-color:#f0f2f5}.hint-hd{text-align:center;font-family:cursive;font-size:150px;font-weight:bold;letter-spacing:5px}.hint-hd span{color:#f56c6c;cursor:pointer;text-shadow:0px 0px 2px#686868,0px 1px 1px#ddd,0px 2px 1px#d6d6d6,0px 3px 1px#ccc,0px 4px 1px#c5c5c5,0px 5px 1px#c1c1c1,0px 6px 1px#bbb,0px 7px 1px#777,0px 8px 3px rgba(100,100,100,0.4),0px 9px 5px rgba(100,100,100,0.1),0px 10px 7px rgba(100,100,100,0.15),0px 11px 9px rgba(100,100,100,0.2),0px 12px 11px rgba(100,100,100,0.25),0px 13px 15px rgba(100,100,100,0.3);-webkit-transition:all.1s linear;transition:all.1s linear}.hint-hd span:hover{text-shadow:0px 0px 2px#686868,0px 1px 1px#fff,0px 2px 1px#fff,0px 3px 1px#fff,0px 4px 1px#fff,0px 5px 1px#fff,0px 6px 1px#fff,0px 7px 1px#777,0px 8px 3px#fff,0px 9px 5px#fff,0px 10px 7px#fff,0px 11px 9px#fff,0px 12px 11px#fff,0px 13px 15px#fff;-webkit-transition:all.1s linear;transition:all.1s linear}.hint-bd{text-align:center;color:#666;font-family:cursive;font-size:20px;text-shadow:0 1px 0#fff;letter-spacing:1px}.hint-ft{text-align:center;padding-top:40px;font-size:14px}.hint-ft.skip{color:#66b1ff}.hint-ft.skip:hover{color:#626fff}</style>");
        sb.append("</head>");

        sb.append("<body>");
        sb.append("<div class=\"err-pg-box\" id=\"error_page_box\">");
        sb.append("<div class=\"hint-hd\">").append(body).append("</div>");
        sb.append("<div class=\"hint-bd\"><div>").append(myErrMsg).append("</div></div>");
        sb.append("<div class=\"hint-ft\"></div>");
        sb.append("</div>");
        sb.append("<script type=\"application/javascript\">(function(_win){function throttle(fn,delay,atleast){var timer=null;var previous=null;return function(){var now=+new Date();if(!previous)previous=now;if(now-previous>atleast){fn();previous=now}else{clearTimeout(timer);timer=setTimeout(function(){fn()},delay)}}}function errPageHeightChange(){var el=_win.document.getElementById(\"error_page_box\");var boxHeight=el.clientHeight;var elPt=el.style.paddingTop;elPt=!elPt?'0':elPt;boxHeight=boxHeight-parseInt(elPt);var docHeight=_win.document.documentElement.clientHeight;if(boxHeight<docHeight){var m=(docHeight-boxHeight)/2-40;if(m<0){return}el.style.paddingTop=m+'px'}}errPageHeightChange();_win.onresize=throttle(errPageHeightChange,100,100);_win.onload=errPageHeightChange})(window);</script>");
        sb.append("</body>");
        sb.append("</html>");

        MyErrorHtmlView myErrorHtmlView = new MyErrorHtmlView(sb.toString());
        return new ModelAndView(myErrorHtmlView);
    }

    public static class MyErrorHtmlView implements View {
        private final String html;

        MyErrorHtmlView(String html) {
            this.html = html;
        }

        @Override
        public String getContentType() {
            return MediaType.TEXT_HTML_VALUE;
        }

        @Override
        public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
            response.setContentType(getContentType());
            byte[] bytes = html.getBytes(StandardCharsets.UTF_8);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            FileCopyUtils.copy(inputStream, response.getOutputStream());
        }
    }

}
